import { createStore, applyMiddleware } from "redux";
// 引入 redux devtools
import { composeWithDevTools } from "@redux-devtools/extension";

// 引入 中间件
import logger from "redux-logger";
// import { thunk } from "redux-thunk";

/**
 * 自定义一个 thunk 中间件
 */
const thunk = (storeApi) => (next) => (action) => {
  if (typeof action === "function") {
    // 执行该函数，且将 dispatch 与 getState 做为参数传递过去
    action(storeApi.dispatch, storeApi.getState);
  } else {
    // 就是一个普通动作，就交给 next 即可
    return next(action);
  }
};

// 引入 reducer
import reducer from "./reducer";

// 创建 store
const store = createStore(
  reducer,
  composeWithDevTools(applyMiddleware(logger, thunk))
);

export default store;
